package com.dctp.ems.controller;

import com.alibaba.fastjson.JSONObject;
import com.dctp.common.controller.BaseController;
import com.dctp.common.utils.DateUtil;
import com.dctp.common.utils.StringUtil;
import com.dctp.common.vo.JsonResult;
import com.dctp.common.vo.PageResult;
import com.jfinal.aop.Before;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.ArrayList;
import java.util.List;

@Path("/compensateConfig")
public class CompensateConfigController extends BaseController {

    /**
     * GET /activity/getTrainConfig
     * 获取活动手数配置列表
     * 参数：activity 活动ID
     */
    public void get() {
        String symbol = getPara("symbol");
        if (StringUtil.isEmpty(symbol)) {
            renderJson(JsonResult.failWithMessage("参数缺失"));
            return;
        }
        List<Record> list = Db.find("SELECT * FROM dctp_compensate_config WHERE symbol = ? ORDER BY down_limit ASC", symbol);
        renderJson(JsonResult.successWithData(list));
    }

    /**
     * POST /activity/setTrainConfig
     * 批量新增或更新活动手数配置
     * 请求体：{ activity: 活动ID, configs: [{down_limit, up_limit, quantity, id?}] }
     */
    @Before(Tx.class)
    public void set() {
        Record body = jsonBody();
        String  symbol = body.getStr("symbol");
        List<JSONObject> configs = body.get("configs");
        if (StringUtil.isEmpty(symbol)) {
            renderJson(JsonResult.failWithMessage("参数缺失"));
            return;
        }
        // 先删除该活动下所有配置
        Db.update("DELETE FROM dctp_compensate_config WHERE symbol = ?", symbol);
        // 批量插入
        for (JSONObject config : configs) {
            config.put("symbol", symbol);
            config.remove("id"); // 保证自增
            config.put("create_time", DateUtil.now());
            Db.save("dctp_compensate_config", new Record().put(config.getInnerMap()));
        }
        renderJson(JsonResult.success());
    }


    public void noticeList() {

        int page = getInt("currentPage", 1);
        int pageSize = getInt("pageSize", 10);
        String title = getPara("title", "");
        StringBuilder where = new StringBuilder(" WHERE 1=1 ");
        List<Object> params = new ArrayList<>();
        if (!title.isEmpty()) {
            where.append(" AND title LIKE ?");
            params.add('%' + title + '%');
        }

        Integer direction = getParaToInt("direction");
        if (null != direction) {
            where.append(" AND direction = ?");
            params.add(direction);
        }

        String symbol = getPara("symbol");
        if (StringUtil.isNotEmpty(symbol)) {
            where.append(" AND symbol = ?");
            params.add(symbol);
        }

        String startTime = getPara("start_time");
        String endTime = getPara("end_time");

        if (StringUtil.isNotEmpty(startTime)) {
            where.append(" AND create_time >= ?");
            params.add(startTime);
        }
        if (StringUtil.isNotEmpty(endTime)) {
            where.append(" AND create_time <= ?");
            params.add(endTime);
        }

        String select = "SELECT * ";
        String from = "FROM dctp_compensate_notice " + where + " ORDER BY id DESC";
        var p = Db.paginate(page, pageSize, select, from, params.toArray());
        renderJson(JsonResult.successWithData(
                new PageResult<>(p.getList(), p.getTotalRow(), pageSize, page)
        ));
    }
}
